比率の信頼区間

正規近似に基づく信頼区間

n <- 100
x <- 20
r <- x/n
se <- (r * (1 - r)/n)^0.5
ci <- r + qnorm(0.975) * se * c(-1, 1)
prop.test(x, n)
## 
##  1-sample proportions test with continuity correction
## 
## data:  x out of n, null probability 0.5 
## X-squared = 34.81, df = 1, p-value = 3.635e-09
## alternative hypothesis: true p is not equal to 0.5 
## 95 percent confidence interval:
##  0.1292 0.2943 
## sample estimates:
##   p 
## 0.2 
## 
binom.test(x, n)
## 
##  Exact binomial test
## 
## data:  x and n 
## number of successes = 20, number of trials = 100, p-value =
## 1.116e-09
## alternative hypothesis: true probability of success is not equal to 0.5 
## 95 percent confidence interval:
##  0.1267 0.2918 
## sample estimates:
## probability of success 
##                    0.2 
## 
library(binom)
## Loading required package: lattice
binom.confint(x, n)  # 11種類の信頼区間
##           method  x   n  mean  lower  upper
## 1  agresti-coull 20 100 0.200 0.1326 0.2896
## 2     asymptotic 20 100 0.200 0.1216 0.2784
## 3          bayes 20 100 0.203 0.1308 0.2863
## 4        cloglog 20 100 0.200 0.1283 0.2832
## 5          exact 20 100 0.200 0.1267 0.2918
## 6          logit 20 100 0.200 0.1328 0.2898
## 7         probit 20 100 0.200 0.1310 0.2872
## 8        profile 20 100 0.200 0.1298 0.2854
## 9            lrt 20 100 0.200 0.1297 0.2854
## 10     prop.test 20 100 0.200 0.1292 0.2943
## 11        wilson 20 100 0.200 0.1334 0.2888

二項分布に基づく母比率の信頼区間のグラフ

n <- 100
x <- 0:n
ci <- sapply(x, function(x) {
    c(x/n, binom.test(x, n)$conf.int[1:2])
})
library(plotrix)
plotCI(x/n, ci[1, ], ci[3, ] - ci[1, ], ci[1, ] - ci[2, ])

plot of chunk r09b